<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>虚线小动画</title>
  </head>
  <body>
    <script>
      (() => {
        const canvas = document.createElement("canvas");
        canvas.width = 400;
        canvas.height = 400;
        document.body.append(canvas);

        const ctx = canvas.getContext("2d");

        ctx.lineWidth = 10;
        ctx.fillStyle = "#fac";
        ctx.moveTo(100, 100);
        ctx.lineTo(100, 200);
        ctx.lineTo(200, 200);
        // ctx.lineTo(100, 100) 手动闭合
        ctx.closePath();
        ctx.stroke();
        ctx.fill();
      })();
    </script>
  </body>
</html>
